'use strict'

const titbit = require('titbit')
const {cookie,session} = require('titbit-toolkit')

const token = require('titbit-token')

let tok = new token({
    // token 有效期，单位是s 
    expires:30,
    // 加解密的密钥 32个字符
    key:'abcdefghijklmnopqrstuvwxyz123456',
    // 必须是16位字母数字
    iv:'chenyanwei126000'

})

const app = new titbit({
    debug:true
})

app.use(new cookie)
    .use(new session)
// session是基于cookie来实现的

app.use(async (c, next)=>{
    c.setHeader('x-test-key',`${Math.random()}`)
    await next()
})

app.use(async (c, next)=>{
    if(c.query.say === '12345'){
        await next()
    }else{
        c.status(403).send('deny')
    }
}, {name:['home'], method:['GET']})
// 对post请求验证的功能

app.get('/', async c=>{
    c.send('ok')
}, {name:'home', group:'home'})

// {name:'home', group:'home'}) 
// 表示给路由起了名字，不同的分组命名，写一个就行

app.get('/test', async c=>{
    c.send('test is ok')
})

// app.get('/:x/:y', async c=>{
//     c.setSession('x', c.param.x)
//     c.setSession('y', c.param.y)
//     c.send( c.getSession() )
// })

app.get('/token', async c=>{
    let tk = tok.make({
        id:'123',
        user:'node',
        level:12
    })

    c.send( tk )
})

app.get('/decrypt/:token',async c=>{
    c.send( tok.verify(c.param.token) )
})

app.run(1235)
